package com.brainhealth.web.controller.customer;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.brainhealth.common.annotation.Anonymous;
import com.brainhealth.common.utils.DateUtils;
import com.brainhealth.customer.domain.Customer;
import com.brainhealth.customer.domain.Exercise;
import com.brainhealth.customer.domain.vo.CustomerExerciseVO;
import com.brainhealth.customer.domain.vo.CustomerReportVO;
import com.brainhealth.customer.domain.vo.FinishExerciseVO;
import com.brainhealth.utils.CommonUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.brainhealth.common.annotation.Log;
import com.brainhealth.common.core.controller.BaseController;
import com.brainhealth.common.core.domain.AjaxResult;
import com.brainhealth.common.enums.BusinessType;
import com.brainhealth.customer.domain.CustomerExercise;
import com.brainhealth.customer.service.ICustomerExerciseService;
import com.brainhealth.common.utils.poi.ExcelUtil;
import com.brainhealth.common.core.page.TableDataInfo;

/**
 * 会员训练记录Controller
 * 
 * @author wolong
 * @date 2023-05-09
 */
@RestController
@RequestMapping("/customer/customerExercise")
public class CustomerExerciseController extends BaseController
{
    @Autowired
    private ICustomerExerciseService customerExerciseService;

    /**
     * 查询会员训练记录列表
     */
    @PreAuthorize("@ss.hasPermi('customer:customerExercise:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerExercise customerExercise)
    {
        startPage();
        List<CustomerExercise> list = customerExerciseService.selectCustomerExerciseList(customerExercise);
        return getDataTable(list);
    }

    /**
     * 导出会员训练记录列表
     */
    @PreAuthorize("@ss.hasPermi('customer:customerExercise:export')")
    @Log(title = "会员训练记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerExercise customerExercise)
    {
        List<CustomerExercise> list = customerExerciseService.selectCustomerExerciseList(customerExercise);
        ExcelUtil<CustomerExercise> util = new ExcelUtil<CustomerExercise>(CustomerExercise.class);
        util.exportExcel(response, list, "会员训练记录数据");
    }

    /**
     * 获取会员训练记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('customer:customerExercise:query')")
    @GetMapping(value = "/{customerExerciseId}")
    public AjaxResult getInfo(@PathVariable("customerExerciseId") Long customerExerciseId)
    {
        return success(customerExerciseService.selectCustomerExerciseByCustomerExerciseId(customerExerciseId));
    }

    /**
     * 新增会员训练记录
     */
    @PreAuthorize("@ss.hasPermi('customer:customerExercise:add')")
    @Log(title = "会员训练记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerExercise customerExercise)
    {
        return toAjax(customerExerciseService.insertCustomerExercise(customerExercise));
    }

    /**
     * 修改会员训练记录
     */
    @PreAuthorize("@ss.hasPermi('customer:customerExercise:edit')")
    @Log(title = "会员训练记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerExercise customerExercise)
    {
        return toAjax(customerExerciseService.updateCustomerExercise(customerExercise));
    }

    /**
     * 删除会员训练记录
     */
    @PreAuthorize("@ss.hasPermi('customer:customerExercise:remove')")
    @Log(title = "会员训练记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{customerExerciseIds}")
    public AjaxResult remove(@PathVariable Long[] customerExerciseIds)
    {
        return toAjax(customerExerciseService.deleteCustomerExerciseByCustomerExerciseIds(customerExerciseIds));
    }

    /**
     * 会员开始训练时获取训练内容
     *
     * @param
     * @param map
     * @return
     */
    @Anonymous
    @PostMapping("/startCustomerExercise")
    public AjaxResult startCustomerExercise(HttpServletRequest request, @RequestBody Map<String, Object> map)
    {
        Long customerExerciseId = Long.valueOf(map.get("customerExerciseId").toString());
        Exercise exercise = customerExerciseService.startCustomerExercise(request, customerExerciseId);
        if (exercise != null)
        {
            return AjaxResult.success("开始会员训练", exercise);
        }
        return AjaxResult.error("还没到会员训练开始时间！");
    }

    /**
     * 获取当前会员训练信息
     */
    @Anonymous
    @PostMapping("/getCustomerExerciseInfo")
    public AjaxResult getCustomerExerciseInfo(@RequestBody Map<String, Object> map)
    {
        Long customerExerciseId = Long.valueOf(map.get("customerExerciseId").toString());
        CustomerExercise customerExercise = customerExerciseService.selectCustomerExerciseByCustomerExerciseId(customerExerciseId);
        if (customerExercise != null)
        {
            Long timeSecond = DateUtil.between(new Date(), customerExercise.getStartTime(), DateUnit.SECOND);
            customerExercise.setExerciseTime(timeSecond);
            logger.info("游戏时间" + timeSecond, "会员训练对象" + JSON.toJSONString(customerExercise));
            return AjaxResult.success(customerExercise);
        } else
        {
            return AjaxResult.error("获取会员记录失败！");
        }
    }

    /**
     * 完成会员训练内容
     */
    @Anonymous
    @PostMapping("/finishCustomerExercise")
    public AjaxResult finishCustomerExercise(HttpServletRequest request, @RequestBody FinishExerciseVO finishExerciseVO)
    {
        CustomerReportVO result = customerExerciseService.finishCustomerExercise(request, finishExerciseVO);
        if (result.getReportId() != null)
        {
            return AjaxResult.success("完成会员训练成功！", result);
        }
        if (result.getCustomerExerciseId() != null)
        {
            return AjaxResult.success("完成会员训练成功！", result);
        }
        return AjaxResult.error("完成会员训练失败！", result);
    }

    @Anonymous
    @PostMapping("/getCustomerExerciseList")
    public AjaxResult getCustomerExerciseList(HttpServletRequest request)
    {
        Customer customer = CommonUtils.getCustomerByRequest(request);
        List<CustomerExerciseVO> customerExerciseVOList = customerExerciseService.getCustomerExerciseListInfo(customer);
        if (customerExerciseVOList.size() > 0)
        {
            return AjaxResult.success("查询会员训练成功！", customerExerciseVOList);
        }
        return AjaxResult.error("查询会员训练失败！");
    }

}
